#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
import json
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

desired_capabilities = DesiredCapabilities.FIREFOX
desired_capabilities['loggingPrefs'] = {'performance': 'ALL'}


class AutoSpider(object):
    def __init__(self):
        self.init_browder()
        self.company_info_dict = {}

    def init_browder(self):
        options = webdriver.FirefoxOptions()
        options.add_argument("disable-blink-features=AutomationControlled")  # 就是这一行告诉chrome去掉了webdriver痕迹
        self.browser = webdriver.Firefox(executable_path='./geckodriver.exe',
                                    options=options, desired_capabilities=desired_capabilities)
        self.wai = WebDriverWait(self.browser, 20)  # 等待20s后抛出异常
        self.start_year = 2022

    def login_index(self,username="13620209221",pwd='123456'):
        login_btn = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(@class,"loyo-data-terrace-layouts-header-login")]')))
        login_btn.click()

        username_elem = self.wai.until(EC.element_to_be_clickable((By.ID, 'phone')))
        pwd_elem = self.wai.until(EC.element_to_be_clickable((By.ID, "passwd")))
        submit_elem = self.wai.until(EC.element_to_be_clickable(
            (By.XPATH, '//button[contains(@class,"ant-btn-primary")]')))
        # 使用账号密码登陆
        username_elem.send_keys(username)
        pwd_elem.send_keys(pwd)
        submit_elem.click()
        time.sleep(2)

    def login(self,username="13027516139",pwd='2258/2258/'):
        use_pwd_login_btn =  self.wai.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="login-tab"]/a[contains(text(),"密码")]')))
        use_pwd_login_btn.click()


        username_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="password-login_wrapper"]//input[@class="form-control"]')))
        pwd_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="password-login_wrapper"]//input[contains(@class,"password-input")]')))
        # 使用账号密码登陆
        username_elem.send_keys(username)
        pwd_elem.send_keys(pwd)
        submit_elem = self.wai.until(EC.element_to_be_clickable(
            (By.XPATH, '//div[@class="password-login_wrapper"]//button[contains(@class,"login-btn")]')))
        submit_elem.click()
        input('国华快')

    def close_alert(self):
        first_alter_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                 '//div[@class="antd-pro-app-src-pages-application-search-vistor-components-xkb-active-modal-index-arrow-delete"]')))
        first_alter_elem.click()
        alter_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                           '//div[@class="antd-pro-app-src-pages-application-search-vistor-components-favorite-guide-index-close"]')))
        alter_elem.click()
        button_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//button[@class="driver-close-btn"]')))
        button_elem.click()
        time.sleep(1)

    def change_search_param(self,left_times=1,right_times=1):
        search_elem = self.wai.until(EC.element_to_be_clickable((By.ID,'btnAdvanceSearch')))

        if left_times:
            # self.wai.until(EC.element_to_be_clickable((By.XPATH, '//div[contains(@title,"20")]'))).click()
            # self.wai.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="kzz_button-select_root"]/div[contains(@class,"kzz_button-select_btn-customize")]'))).click()

            self.wai.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="kzz_button-select_root"]/div[contains(@class,"kzz_button-select_btn-customize")]'))).click()


            start_time_elem, end_time_elem = self.browser.find_elements_by_xpath('//input[contains(@class,"ant-calendar-picker-input")]')
            start_time_elem.click()
            left_last_year_elem = self.wai.until(EC.element_to_be_clickable(
                (By.XPATH, '//a[contains(@class,"ant-calendar-prev-year-btn")]')))
            for i in range(left_times):
                left_last_year_elem.click()
            left_day_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                       '//td[contains(@class,"ant-calendar-selected-day")]')))
            left_day_elem.click()
            right_last_year_elem = self.wai.until(EC.element_to_be_clickable(
                (By.XPATH, '//a[contains(@class,"ant-calendar-prev-year-btn")]')))
            for i in range(right_times):
                right_last_year_elem.click()
            right_day_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,
                                                                       '//td[contains(@class,"ant-calendar-selected-day")]')))
            right_day_elem.click()

        search_elem.click()
        time.sleep(2)

    def wait_company_info_lode(self):
        self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="ant-spin-nested-loading"]')))

        time.sleep(1)



    # def save_company_info(self):
    #     company_td_elems = self.browser.find_elements_by_xpath('//td[@class="ant-table-selection-column"]/following-sibling::td[1]')
    #     for elem in company_td_elems:
    #         time.sleep(1)
    #         try:
    #             elem.click()
    #             try:
    #                 contact_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(text(),"联系方式")]')))
    #                 contact_tab_elem.click()
    #                 mobile_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(text(),"手机")]')))
    #                 mobile_tab_elem.click()
    #                 get_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//button[contains(@class,"ant-btn-primary")]/span[contains(text(),"领取线索")]/..')))
    #                 get_elem.click()
    #
    #                 ActionChains(self.browser).move_to_element(elem).perform()
    #             except:
    #                 print('no source')
    #             file_name = '精线索/' + str(time.time())
    #             with open(file_name, 'w', encoding='utf-8') as f:
    #                 f.write(self.browser.page_source)
    #                 self.parse_source_code(self.browser.page_source)
    #         except:
    #             print('error')

    def save_company_info(self):
        company_td_elems = self.browser.find_elements_by_xpath('//div[@class="ant-table-fixed-left"]//tbody[@class="ant-table-tbody"]/tr')
        is_end = False
        for index,elem in enumerate(company_td_elems):
            if index > 20:
                break
            while True:
                try:
                    time.sleep(2)
                    elem.click()
                    print('移到')
                    break
                except:
                    print('翻到底了，跳过')

            time.sleep(3)
            from lxml import etree
            selector = etree.HTML(self.browser.page_source, parser=etree.HTMLParser(encoding='utf-8'))
            company_name = ''.join(selector.xpath('//div[@class="index-module--k6MBQ"]/span/h1/text()')).strip()

            contact_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"ant-tabs-tab") and contains(string(),"联系方式")]')))
            contact_tab_elem.click()
            mobile_tab_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH,'//label[contains(@class,"ant-radio-button-wrapper") and contains(string(),"手机")]')))
            mobile_tab_elem.click()
            time.sleep(2)
            file_name = '客找找/' + str(time.time())
            with open(file_name, 'w', encoding='utf-8') as f:
                f.write(self.browser.page_source)
                self.parse_source_code(self.browser.page_source,company_name)
            company_td_elems = self.browser.find_elements_by_xpath(
                '//div[@class="customer-information-text"]')
            close_elem = self.wai.until(EC.element_to_be_clickable(
                (By.XPATH, '//div[@class="ant-drawer-header-no-title"]/button[@class="ant-drawer-close"]')))
            close_elem.click()
            time.sleep(5)


    def parse_source_code(self,text,company_name):
        from lxml import etree
        selector = etree.HTML(text,parser=etree.HTMLParser(encoding='utf-8'))
        tr_selectors = selector.xpath('//div[@class="index-module--zMXYF"]//tbody[@class="ant-table-tbody"]/tr')
        if not self.company_info_dict.get(company_name,{}):
            self.company_info_dict[company_name] = {
                'phone_ls':[]
            }
        self.company_info_dict[company_name]['start_year'] = self.start_year
        phone_count = 0
        save_ls = []
        for div_selector in tr_selectors:
            phone = ''.join(div_selector.xpath('./td//li[@class="index-module--PAD2j"]/span/text()')).strip()
            source = ''.join(div_selector.xpath('./td[@class=""]/text()'))
            source_ls = [source]
            source_ls = [i for i in source_ls if i]
            source_href_ls = []
            save_ls.append([source_ls, source_href_ls, phone])
            self.company_info_dict[company_name]['phone_ls'].append({
                "phone":phone,
                'source_ls':source_ls,
                'source_href_ls': source_href_ls
            })
            phone_count += 1
            print(company_name,phone,source_ls,source_href_ls)
            print('==============================')
        self.company_info_dict[company_name]['phone_count'] = phone_count
        for ls in save_ls:
            with open('jz_info_kzz.csv', 'a', encoding='utf-8') as f:
                start_year = self.company_info_dict[company_name].get('start_year')
                f.write(f'{company_name},{start_year},{phone_count},{ls[2]},{str(ls[0])},{str(ls[1])}\n')
    def main(self):
        self.browser.maximize_window()
        self.browser.get('https://www.qcc.com/weblogin?back=https%3A%2F%2Fwww.kezhaozhao.com%2Fqcc%2Fkzz%2Ftransit')
        # 登录
        self.login()
        self.browser.get('https://www.kezhaozhao.com/search?tab=advance')
        tmplete_elem = self.wai.until(
            EC.element_to_be_clickable((By.XPATH, '//div[@class="kzz_filter_row-selections"]/span')))
        tmplete_elem.click()
        # 使用已保存高晒条件
        while True:
            try:
                tmplete_ls_elem = self.wai.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="ant-drawer-body"]/div')))
                # tmplete_ls_elem = self.wai.until(EC.invisibility_of_element((By.XPATH, '//div[@class="ant-drawer-body"]/div')))
                print('found')
                break
            except:
                print('not found')
                self.browser.get('https://www.kezhaozhao.com/topic-search')
                time.sleep(5)
                self.browser.get('https://www.kezhaozhao.com/search?tab=advance')
        search_elem = self.wai.until(EC.element_to_be_clickable((By.ID, 'btnAdvanceSearch')))
        ActionChains(self.browser).move_to_element(search_elem).move_by_offset(1294, -433).click().perform()
        time.sleep(2)
        div_emem_ls = self.browser.find_elements_by_xpath('//div[@class="template-drawer__list-item"]')
        for div_elem in div_emem_ls:
            print("正在翻模块")
            self.browser.execute_script("arguments[0].scrollIntoView();", div_elem)
            ActionChains(self.browser).move_to_element(div_elem).click().perform()
            # div_elem.click()
            try:
                search_elem = self.wai.until(EC.element_to_be_clickable((By.ID, 'btnAdvanceSearch')))
                search_elem.click()
            except Exception as e:
                print(e)
                time.sleep(3)
            from lxml import etree
            selector = etree.HTML(self.browser.page_source, parser=etree.HTMLParser(encoding='utf-8'))
            self.start_year = ''.join(selector.xpath('//div[@class="ant-select-selection-selected-value" and contains(@title,"20")]/@title')).strip()[:4]
            print(f'当前年份{self.start_year}')
            self.wait_company_info_lode()
            self.save_company_info()
            time.sleep(3)
            js = 'var action=document.documentElement.scrollTop=0'
            self.browser.execute_script(js)
            search_elem = self.wai.until(EC.element_to_be_clickable((By.ID, 'btnAdvanceSearch')))
            time.sleep(2)
            ActionChains(self.browser).move_to_element(search_elem).move_by_offset(1294, -433).click().perform()
            div_emem_ls = self.browser.find_elements_by_xpath('//div[@class="template-drawer__list-item"]')
            print('再来一轮')

        with open('result_kzz.json','w',encoding='utf-8') as f:
            json.dump(self.company_info_dict,f,ensure_ascii=False,indent=4)


if __name__ == '__main__':
    spider = AutoSpider()
    spider.main()
